package com.mef.smsplat.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.CompanyInfoDomain;

@Repository
public class CompanyInfoDao extends BaseSpringJdbcDao<CompanyInfoDomain, String> {
	
	/**
	 * 根据公司名 OFFICE IADA分页查询信息
	 * @param companyName
	 * @param office
	 * @param iada
	 * @param dgm
	 * @return
	 */
	public Page query(String companyName,String office,String phone,String groupId,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from company_info c left join group_menber_info g on c.company_id=g.menber_id where 1=1");
		if(companyName!=null&&!"".equals(companyName)){
			sql.append(" and c.company_name like ?");
			params.add("%"+companyName+"%");
		}
		if(office!=null&&!"".equals(office)){
			sql.append(" and c.office like ?");
			params.add("%"+office+"%");
		}
		if(phone!=null&&!"".equals(phone)){
			sql.append(" and c.person_phone like ?");
			params.add("%"+phone+"%");
		}
		if(groupId!=null&&!"".equals(groupId)){
			sql.append(" and g.group_id=?");
			params.add(groupId);
		}
		Page<CompanyInfoDomain> page= super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
		return page;
	}
	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return CompanyInfoDomain.class;
	}
	
	/**
	 * 批量处理
	 * @param ids
	 * @return
	 */
	public void deleteCompanyMember(String[] ids){
		String sql="delete from group_menber_info where menber_id=?";
		for (int i = 0; i < ids.length; i++) {
			super.getJdbcTemplate().update(sql, new Object[]{ids[i]});
		}
	}
	
	/**
	 * 根据组id 分页查询成员信息
	 * @param groupId
	 * @param dgm
	 * @return
	 */
	public Page queryMember(String groupId,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from company_info c left join group_menber_info g on c.company_id=g.menber_id where g.group_id=?");
		params.add(groupId);
		Page<CompanyInfoDomain> page= super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
		return page;
	}


}
